package xyz.twj.micro.service.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.twj.micro.service.dao.UserRepository;
import xyz.twj.micro.service.enties.UserEntity;
import xyz.twj.utils.encapsulation.ResultUtils;

import javax.persistence.EntityManager;
import java.util.Date;
import java.util.Map;

/**
 * @author : TianWenjian
 */
@RestController
public class DemoController {

    @Autowired
    UserRepository userRepository;

    @RequestMapping("/message")
    public String message(){
        return "hello micro servi  ce         ";
    }

    @GetMapping("/micro-service")
    public Map service(){

        return ResultUtils.succeed("ok", userRepository.findAll());
        //return "hello micro service2   ";
    }

    @GetMapping("/add")
    public Map add(){
        UserEntity userEntity = new UserEntity();
        userEntity.setBirthday(new Date());
        userEntity.setName("小星");

        userRepository.save(userEntity);
        return ResultUtils.succeed("ok");
    }

    @GetMapping("/update")
    public Map update(){
        UserEntity userEntity = new UserEntity();
        userEntity.setBirthday(new Date());
        userEntity.setName("小刚");
        userEntity.setId(2);

        userRepository.save(userEntity);
        return ResultUtils.succeed("ok");
    }
}
